<template>
	<div class="send-code" :class="className" @click="sendClick">{{canGet ? '发送验证码' : '重新发送'+(timer)+'秒'}}</div>
</template>

<script>
	var lodash = require('lodash');
	import { sendSMSCode } from 'api/common/common';
	import { checkPhoneExist } from 'api/register';
	export default {
		props: {
			// 倒计时的时间
			codeTime: {
				type: [Number,String],
				default: 60
			},
			// 获取短信验证码的接口参数的来源（source） 必填
			codeType: {
				type: String
			},
			//手机号码， 看短信验证码的接口的 source
			codeMobile: {
				type: [Number,String],
			},
			// 短信验证码的接口参数
			params: {
				type: Object
				
			},
			className: {
				type: String
			}
		},
		data() {
			return {
				timer: this.codeTime,
				intervalId: null,
				canGet: true,
				isRegistere: false
			}
		},
		computed: {
			//检测手机号码是否正确
			phoneRight() {
			  return /^1[0-9][0-9]{9}$/.test(this.codeMobile);
			},
		},
		onLoad() {
			
		},
		methods: {
			sendClick() {
				let that = this
				let params = {
					source: this.codeType
				}
				// 发送来源, reg: 注册, smsLogin: 手机号登录, forget: 忘记密码, updatePhone: 修改手机号, withdraw: 提现, updatePasswd: 修改登录密码, updatePayPasswd, bindPhone: 绑定手机号, self: 自己, other: 其他
				if(!(this.codeType == 'withdraw' || this.codeType == 'updatePasswd' || this.codeType == 'updatePhone' || this.codeType == 'self')) {
					// 手机号码, 当来源是提现, 修改密码, 修改手机号, 自己时不需要传
					if (this.codeMobile) {
						if (this.phoneRight === false) {
							this.$toast({
								message: '请输入正确的手机号~~~',
							})
							return
						}
					} else{
						this.$toast({
							message: '请输入手机号~~~',
						})
						return
					}
					
					params.phoneNumber = this.codeMobile
				}  
				else if(this.codeType == 'reg') {
					
					let params = {
						phone: this.codeMobile
					}  


					// if (!this.isRegistere) {
					// 	this.checkPhoneExist1(params).then(res => {
					// 		if (res.status == 1) {
					// 			this.isRegistere = true
								
					// 		} else {
					// 			this.isRegistere = false
					// 		}
					// 	}).catch(e => {
					// 		this.isRegistere = true
					// 	})
				

					// } else {
					// 	return
					// }

					 

				}
			
				params = lodash.merge(this.params, params);
				
				
				if (this.canGet) {

					this.sendSMSCodeP(params).then(res => {
						if (res.status == 1) {
							this.canGet = false
							this.$emit('sendCode', res.result)
							// 开启倒计时
							this.timer = this.codeTime
							this.intervalId = setInterval(() => {
								this.timer --
								if (this.timer == 0) {
									clearInterval(this.intervalId);
									this.canGet = true
								}
							}, 1000);
						} else{
							this.$toast({
								message: res.msg,
							})
						}
					})

					// sendSMSCode(params).then(res => {
						
					// 	if (res.status == 1) {
					// 		this.canGet = false
					// 		// 开启倒计时
					// 		this.timer = this.codeTime
					// 		this.intervalId = setInterval(() => {
					// 			this.timer --
					// 			if (this.timer == 0) {
					// 				clearInterval(this.intervalId);
					// 				this.canGet = true
					// 			}
					// 		}, 1000);
					// 	} else{
					// 		this.$toast({
					// 			message: res.msg,
					// 		})
					// 	}
					// })
				}
				
				
				
			},
			checkPhoneExist1(params) {


				return new Promise((resolve, reject) => {
					checkPhoneExist(params).then(res => {
						if (res.status == 1) {
							res.success = true;
							resolve(res);
						} else {
							res.success = false;
							resolve(res);
						}
					}).catch(e => {
						res.success = false;
						reject(res);
					});
				});
				
			},
			sendSMSCodeP(params) {
				return new Promise((resolve, reject) => {
					sendSMSCode(params).then(res => {
						if (res.status == 1) {
							res.success = true;
							resolve(res);
						} else {
							res.success = false;
							resolve(res);
						}
					}).catch(e => {
						e.success = false;
						e.msg = "网络请求异常!";
						reject(e);
					})
				})
			},
			//清除时间
			clearTimer() {
				clearInterval(this.intervalId);
			}
		},
		destroyed() {
			clearInterval(this.intervalId);
		}
	}
</script>

<style>
</style>
